//该文件用于生成替换hthread库的替换动态库，该动态库中对hthread中的所有同名函数给出不同的实现，用以实现CPU端和模拟器端的同步
#include"hthread_sub.h"

int hthread_dev_open(int cluster_id) {
#ifdef TEST
    printf("hthread_dev_open called!!! cluster_id: %d\n", cluster_id);
#endif
    return global_dsp_chip->open_dev(cluster_id, getpid());
}

// int hthread_dev_owner(int cluster_id) {
// #ifdef TEST
//     printf("hthread_dev_owner called!!! cluster_id: %d\n", cluster_id);
// #endif
//     return global_dsp_chip->dev_owner(cluster_id);
// }

int hthread_dev_close(int cluster_id) {
#ifdef TEST
    printf("hthread_dev_close called!!! cluster_id: %d\n", cluster_id);
#endif
    return global_dsp_chip->close_dev(cluster_id);
}

int hthread_dat_load(int cluster_id, char* filePath) {
#ifdef TEST
    printf("hthread_dat_load called!!! cluster_id: %d, filePath: %s\n", cluster_id, filePath);
#endif 
    // global_dsp_chip->init_Cluster(cluster_id);
    //调用parsing接口，将分析后的dat文件存入对应id的cluster的instruction memory
    instr_unit_parsing(filePath, cluster_id, global_dsp_chip);
}

int hthread_dat_unload(int cluster_id) {
#ifdef TEST
    printf("hthread_dat_unload called!!! cluster_id: %d\n", cluster_id);
#endif  
    //clear the instruction memory of the cluster
    // global_dsp_chip->destroy_Cluster(cluster_id);
    //instr_unit_clear(cluster_id);
}

int hthread_group_create(int cluster_id, int num) {
#ifdef TEST
    printf("hthread_group_create called!!! cluster_id: %d, num: %d\n", cluster_id, num);
#endif
    return global_dsp_chip->get_cluster(cluster_id)->group_create(num);
    
}

int hthread_group_create(int cluster_id, int num, char *func_name, int scalarArgs, int ptrArgs, uint64_t *argArray) {
#ifdef TEST
    printf("hthread_group_create called!!! cluster_id: %d, num: %d, func_name: %s, scalarArgs: %d, ptrArgs: %d\n", cluster_id, num, func_name, scalarArgs, ptrArgs);
#endif  
    return global_dsp_chip->get_cluster(cluster_id)->group_create(num, func_name, scalarArgs, ptrArgs, argArray);
}

int hthread_group_masked_create(int cluster_id, unsigned int pmask, char *func_name, int scalarArgs, int ptrArgs, uint64_t *argArray) {
#ifdef TEST
    printf("hthread_group_masked_create called!!! cluster_id: %d, pmask: %u, func_name: %s, scalarArgs: %d, ptrArgs: %d\n", cluster_id, pmask, func_name, scalarArgs, ptrArgs);
#endif  
    return global_dsp_chip->get_cluster(cluster_id)->group_masked_create(pmask, func_name, scalarArgs, ptrArgs, argArray);
}

int hthread_group_exec(int thread_id, char *func_name, int scalarArgs, int ptrArgs, uint64_t *argArray) {
#ifdef TEST
    printf("hthread_group_exec called!!! thread_id: %d, func_name: %s, scalarArgs: %d, ptrArgs: %d\n", thread_id, func_name, scalarArgs, ptrArgs);
#endif
    
}

//TODO:目前模拟器和CPU端串行，因此该接口目前无法准确还原（CPU端执行到该API时，模拟器一定已经执行完所有代码，处于空闲状态）
/* Get the status of a thread group */
int hthread_group_get_status(int thread_id) {
#ifdef TEST
    printf("hthread_group_get_status called!!! thread_id: %d\n", thread_id);
#endif
}

//TODO：目前模拟器和CPU端串行，因此该接口目前无需标志位判断，直接返回（CPU端执行到该API时，模拟器一定已经执行完所有代码，处于空闲状态）
/* Wait for a thread group to finish */
void hthread_group_wait(int thread_id) {
#ifdef TEST
    printf("hthread_group_wait called!!! thread_id: %d\n", thread_id);
#endif
}

/* Destroy a thread group */
int hthread_group_destroy(int thread_id) {
#ifdef TEST
    printf("hthread_group_destroy called!!! thread_id: %d\n", thread_id);
#endif
}

/* Get the number of available threads in a cluster */
int hthread_get_avail_threads(int cluster_id) {
#ifdef TEST
    printf("hthread_get_avail_threads called!!! cluster_id: %d\n", cluster_id);
#endif
}

/*2.4 Device Memory Management*/

/* Allocate memory for a thread */
void *hthread_malloc(int cluster_id, int bytes, int mode) {
#ifdef TEST
    printf("hthread_malloc called!!! cluster_id: %d, bytes: %d, mode: %d\n", cluster_id, bytes, mode);
#endif
}

void hthread_free(void *ptr) {
#ifdef TEST
    printf("hthread_free called!!! ptr: %p\n", ptr);
#endif  
}

/*2.5 Thread Synchronization*/

int hthread_barrier_create(int cluster_id) {
#ifdef TEST
    printf("hthread_barrier_create called!!! cluster_id: %d\n", cluster_id);
#endif
}

int hthread_barrier_destroy(int barrier_id) {
#ifdef TEST
    printf("hthread_barrier_destroy called!!! barrier_id: %d\n", barrier_id);
#endif
}

int hthread_rwlock_create(int cluster_id) {
#ifdef TEST
    printf("hthread_rwlock_create called!!! cluster_id: %d\n", cluster_id);
#endif
}